package ee.mtiidla.tuk;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import ee.mtiidla.data.Event;
import ee.mtiidla.data.Lenex;
import ee.mtiidla.data.Meet;
import ee.mtiidla.data.Session;
import ee.mtiidla.tuk.background.LenexReader;
import ee.mtiidla.tuk.background.SlideWriter;
import ee.mtiidla.tuk.ui.MainFrame;
import ee.mtiidla.tuk.ui.MainFrame.UICallback;

/**
 * Created by Marko on 12.10.2014.
 */
public class Application implements UICallback {

	private File mLastReadDirectory;
	private File mInputLenexFile;
	private File mInputTemplateFile;

	private Lenex mLenex;

	private MainFrame mMainFrame;


	public Application() {

		mMainFrame = MainFrame.create(this);

	}

	@Override
	public void onOpenLenexClick() {

		File file = pickFileToRead("Lenex");
		if (file != null) {
			mInputLenexFile = file;
			mLastReadDirectory = file.getParentFile();
			onReloadClick();
		}

	}

	@Override
	public void onOpenTemplateClick() {

		File file = pickFileToRead("HTML template");
		if (file != null) {
			mInputTemplateFile = file;
		}
	}

	@Override
	public void onReloadClick() {

		if (mInputLenexFile == null) {
			onOpenLenexClick();
		} else {
			processLenexFile(mInputLenexFile);
		}

	}

	@Override
	public void onGenerateClick(List<Long> selectedEvents) {

		if (mLenex == null) {
			onOpenLenexClick();
		} else {
			Meet meet = mLenex.getMeets().get(0);

			List<Event> selected = new ArrayList<Event>();

			for (Session session : meet.getSessions()) {
				for (Event event : session.getEvents()) {
					if (selectedEvents.contains(event.getEventId())) {
						selected.add(event);
					}
				}
			}

			processSelectedEvents(mLenex, selected);

		}

	}

	public void processSelectedEvents(Lenex lenex, List<Event> selectedEvents) {

		new SlideWriter(mInputLenexFile.getParentFile(), mInputTemplateFile, lenex, selectedEvents,
				mMainFrame.getRankingOrder(), mMainFrame.getAgeGroupOrder()) {
			@Override
			public void onOutputDone(Lenex lenex) {

			}

			@Override
			public void onError(Exception e) {
				JOptionPane.showMessageDialog(mMainFrame, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}.start();

	}

	public void processLenexFile(File input) {

		new LenexReader(input) {

			@Override
			public void onLenexRead(Lenex lenex) {
				mMainFrame.setSessions(lenex.getMeets().get(0).getSessions());
				mLenex = lenex;
			}

			@Override
			public void onError(Exception e) {
				JOptionPane.showMessageDialog(mMainFrame, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}

		}.start();

	}



	public File pickFileToRead(String fileType) {

		final JFileChooser fc = new JFileChooser();
		fc.setDialogTitle("Open " + fileType + " file");

		if (mLastReadDirectory != null) {
			fc.setCurrentDirectory(mLastReadDirectory);
		}
		int result = fc.showOpenDialog(mMainFrame);

		if (result == 1) {
			return null;
		}

		return fc.getSelectedFile();
	}
}
